/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.opshowcase.messagecontainer.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.openparts.OpenParts.client.OPCommandCallback;
import fr.openparts.OpenParts.client.OPCommandFailureReason;
import fr.openparts.OpenParts.client.OPMessageFromEmbeddedViewHandler;
import fr.openparts.OpenParts.client.OPParams;
import fr.openparts.OpenParts.client.OPSlot;
import fr.openparts.opshowcase.messagecontainer.client.presenter.MessageContainerPresenter;
import fr.openparts.opshowcase.messagecontainer.client.resources.MessageContainerResources;

/**
 * Embedded view container for Messages Dispatching 
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 27 January 2011
 */
public class MessageContainerEmbeddedView extends Composite {

    interface MessageContainerEmbeddedViewUiBinder extends UiBinder<Widget, MessageContainerEmbeddedView> {}
    private static MessageContainerEmbeddedViewUiBinder uiBinder = GWT.create(MessageContainerEmbeddedViewUiBinder.class);

    protected MessageContainerPresenter presenter;
    
    @UiField
    protected MessageContainerResources resources;
    
    @UiField
    protected LayoutPanel content;
    @UiField
    protected Label message;
    @UiField
    protected Button displayView;
    @UiField
    protected Button sendMessage;
    @UiField
    protected Label callback;
    
    protected int id;
    protected OPSlot slot;
    
    /**
     * Constructor.
     */
    public MessageContainerEmbeddedView(final MessageContainerPresenter presenter, final int id) {
        initWidget(uiBinder.createAndBindUi(this));
        resources.style().ensureInjected();
        this.presenter = presenter;
        this.id = id;
        sendMessage.setEnabled(false);
        message.setText("no message from view " + id + " yet");
    }
    
    /**
     * Display the embedded view
     */
    @UiHandler("displayView")
    public void displayEmbeddedView(final ClickEvent event) {
        presenter.displayEmbeddedView(this);
    }
    
    /**
     * Send a message to the embedded view
     */
    @UiHandler("sendMessage")
    public void sendMessageToEmbeddedView(final ClickEvent event) {
        slot.getViewProxy().dispatchMessageToEmbeddedView("message from container " + id, new OPParams("id", String.valueOf(id)), new OPCommandCallback() {
            @Override
            public void onSuccess() {
                callback.setText("slot dispatch success " + id);
            }
            @Override
            public void onFail(OPCommandFailureReason reason) {
                callback.setText("slot dispatch failure " + id + " : " + reason.getType());
            }
        });
    }
    
    /**
     * Returns the id of this container.
     */
    public int getId() {
        return id;
    }
    
    /**
     * Set the slot for this container.
     */
    public void setSlot(final OPSlot slot) {
        this.slot = slot;
        content.add(slot);
        displayView.setEnabled(false);
        sendMessage.setEnabled(true);
        slot.getViewProxy().addMessageHandler(new OPMessageFromEmbeddedViewHandler() {
            @Override
            public void handleMessageFromEmbeddedView(String msg, OPParams params) {
               if ((params != null) && params.getParamValue("id") != null) {
                   message.setText(msg + " params = " + params.getParamValue("id"));
               } else {
                   message.setText(msg);
               }
            }
            @Override
            public void onEmbeddedViewAttach() {
            }
        });
    }
}
